package top.haijunit.work.core.doamin;

import com.fasterxml.jackson.annotation.JsonInclude;
import io.swagger.v3.oas.annotations.media.Schema;
import lombok.*;
import lombok.experimental.Accessors;

import java.time.Instant;
import java.time.ZonedDateTime;

/**
 * 统一返回Rest风格的数据结构
 *
 * @author zhanghaijun
 * @since 2024-08-07
 */
@Data
@ToString
@NoArgsConstructor
@AllArgsConstructor
@Accessors(chain = true)
@Schema(description = "返回数据结构")
public class Result<T> implements java.io.Serializable {

    @Schema(description = "状态码")
    private Integer code;

    @Schema(description = "提示信息")
    private String message;

    @Schema(description = "返回数据")
    private T data;

    @Schema(description = "请求时间戳")
    private Instant time = ZonedDateTime.now().toInstant();

    public static <T> Result<T> of(Integer code, String message, T data) {
        return new Result<>(code, message, data, ZonedDateTime.now().toInstant());
    }

    public static <T> Result<T> of(ExceptionEnum exception, T data) {
        return of(exception.getCode(), exception.getMessage(), data);
    }

    public static <T> Result<T> success(T data) {
        return of(ExceptionEnum.SUCCESS, data);
    }

    public static Result<Void> success() {
        return of(ExceptionEnum.SUCCESS, null);
    }

    public static <T> Result<T> failed(T data) {
        return of(ExceptionEnum.INTERNAL_SERVER_ERROR, data);
    }
}
